import { constantRoutes } from "@/router";
import { getRouters } from "@/api/menu";
import Layout from "@/layout/layout.js";
import ParentView from "@/components/ParentView";

const permission = {
  state: {
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
  },
  mutations: {
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes;
      state.routes = constantRoutes.concat(routes);
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      state.defaultRoutes = constantRoutes.concat(routes);
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      // 顶部导航菜单默认添加统计报表栏指向首页
      const index = [
        /* {
          path: 'index',
          meta: { title: '统计报表', icon: 'dashboard'}

        } */
      ];
      state.topbarRouters = routes.concat(index);
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes;
    },
  },
  actions: {
    // 生成路由
    // GenerateRoutes({ commit }) {
    //   return new Promise((resolve, reject) => {
    //     // commit('SET_SIDEBAR_ROUTERS', constantRoutes)
    //     // commit('SET_TOPBAR_ROUTES', constantRoutes)
    //     // console.log(JSON.parse(JSON.stringify(constantRoutes)))
    //     // 向后端请求路由数据
    //     getRouters().then(res => {
    //       if (res && res.code === 200) {
    //         let commonRoutes = res.data;

    //         if(!commonRoutes || (commonRoutes && commonRoutes.length === 0)){
    //           reject();
    //         }
    //         handleRoute(commonRoutes)

    //         let allRoutes = constantRoutes.concat(commonRoutes)

    //         const route = allRoutes.reduce((or, oc) => {
    //           or.push({
    //             ...oc,
    //             fullPath: oc.path,
    //             isHiddenIcon: false,
    //             children: oc.children ? oc.children.reduce((tr, tc) => {
    //               tr.push({
    //                 ...tc,
    //                 fullPath: `${oc.path}/${tc.path}`,
    //                 isHiddenIcon: false,
    //                 children: tc.children ? tc.children.reduce((mr, mc) => {
    //                   mr.push({
    //                     ...mc,
    //                     isHiddenIcon: true,
    //                     fullPath: `${oc.path}/${tc.path}/${mc.path}`,
    //                   })
    //                   return mr
    //                 }, []) : null
    //               })
    //               return tr
    //             }, []) : null
    //           })
    //           return or
    //         }, [])

    //         route.push({ path: '*', redirect: '/403', hidden: true })

    //         commit('SET_ROUTES', route)
    //         commit('SET_SIDEBAR_ROUTERS', route)
    //         commit('SET_DEFAULT_ROUTES', route)
    //         commit('SET_TOPBAR_ROUTES', route)
    //         resolve(route)
    //       }
    //     })
    //   })
    // }
    GenerateRoutes({ commit }, roles) {
      return new Promise((resolve) => {
        let accessedRoutes;
        accessedRoutes = constantRoutes || [];
        // if (roles.includes("admin")) {
        //   accessedRoutes = constantRoutes || [];
        // } else {
        //   accessedRoutes = filterAsyncRoutes(asyncRoutes, roles);
        // }
        commit("SET_ROUTES", []);
        resolve(accessedRoutes);
      });
    },
  },
};

/**
 * 处理路由组件
 * @param router
 */
const handleRoute = (router = []) => {
  router.forEach((r) => {
    if (r.component) {
      if (r.component === "Layout") {
        r.component = Layout;
      } else if (r.component === "ParentView") {
        r.component = ParentView;
      } else {
        r.component = requireRouter(r.component);
      }
    }
    if (r.children && r.children instanceof Array) {
      handleRoute(r.children);
    } else {
      r.redirect = null;
    }
  });
};

/**
 * 路由懒加载
 * @param path
 * @returns {function(*): *}
 */
const requireRouter = (path) => (r) =>
  require.ensure([], () => r(require("@/views/" + path + ".vue")));

export default permission;
